home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.onyx.net!claymoor
- From: Adam.Morris@octacon.co.uk (Adam Morris)
- Newsgroups: comp.lang.c++
- Subject: Re: [HLP] Easy Problem w/ Classes!
- Date: Fri, 01 Mar 96 14:52:35 GMT
- Organization: Octacon Ltd
- Message-ID: <4h6hea$3v5@mulgave.octacon.co.uk>
- References: <4gfn3v$jp@news.umbc.edu>
- NNTP-Posting-Host: claymoor.onyx.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4gfn3v$jp@news.umbc.edu>, ssopre1@umbc.edu (Sunil ) wrote:
- >Hello all
- > Heres a little problem that I need help with.
- >
- >// Point.h contains
- >Class Point {
- >private: int x;
- > int y;
- >
- >public: Point();
- >}
- >
- >// LineSeg.h contains
- >class LineSeg{
- >private:
- > Point end;
- > Point start;
- >public:
- > LineSeg();
- >}
- >
- >// LineSeg.C contains
- >LineSeg::LineSeg()
- >{
- > Point.end.x=0; // Are these valid?
- > Point.end.y=0; // What do I need to add/del here?
- > Point.start.x=0;
- > Point.start.y=0;
- >}
- >
- >Basically, LineSeg() is supposed to set starting and ending
- >points to zero. Compiler goes berserkoid .. .
-
- I'm not surprised, you are attempting to access private data members of point.
-
- you either need to make them public (in which case just replace point with a
- struct) or more sensibly add functions to return x and y (try setting them as
- well)
-
- you should end up with
- class Point{
- public:
- Point();
- Point(int x, int y);
-
- void setPoint(int anXValue, int anYValue);
- void setX(int anXValue);
- void setY(int aYValue);
-
- int getX();
- int getY();
- private:
- int theXValue;
- int theYValue;
- };
-
- I'll leave you to write the bodies... (this is incomplete you would probably
- want a copy constructor and an operator= as well)
-
- Then in line seg you can declare your points
-
- Point end(1, 2);
- Point start();
-
- start.set(3, 4);
-
- int aValue;
- aValue = end.getX();
- ..
- and so on.
-
- Adam
-